package com.weetop.cfw.utils

import com.trello.rxlifecycle3.android.ActivityEvent
import com.trello.rxlifecycle3.android.FragmentEvent
import com.trello.rxlifecycle3.components.RxActivity
import com.trello.rxlifecycle3.components.RxDialogFragment
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity
import com.trello.rxlifecycle3.components.support.RxFragment
import com.weetop.cfw.bean.ErrorBean
import com.weetop.cfw.callback.RxJavaCallBack
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 *@Description:RxJava的工具类
 */
class RxJavaUtils {

    companion object{
        /**
         * 在AppCompatActivity中使用RxJava进行网络请求
         */
        fun <T : ErrorBean> useInAppCompatActivity(
            observable: Observable<T>,
            rxAppCompatActivity: RxAppCompatActivity,
            baseCallBack: RxJavaCallBack<T>
        ) {
            observable.compose(rxAppCompatActivity.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(baseCallBack)
        }

        /**
         * 在Activity中使用RxJava进行网络请求
         */
        fun <T : ErrorBean> useInActivity(
            observable: Observable<T>,
            rxActivity: RxActivity,
            baseCallBack: RxJavaCallBack<T>
        ) {
            observable.compose(rxActivity.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(baseCallBack)
        }

        /**
         * 在support包下的Fragment中使用RxJava进行网络请求
         */
        fun <T : ErrorBean> useInSupportFragment(
            observable: Observable<T>,
            rxFragment: RxFragment,
            baseCallBack: RxJavaCallBack<T>
        ) {
            observable.compose(rxFragment.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(baseCallBack)
        }

        /**
         * 在app包下的Fragment中使用RxJava进行网络请求
         */
        fun <T : ErrorBean> useInFragment(
            observable: Observable<T>,
            rxFragment: com.trello.rxlifecycle3.components.RxFragment,
            baseCallBack: RxJavaCallBack<T>
        ) {
            observable.compose(rxFragment.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(baseCallBack)
        }

        /**
         * 在AppCompatActivity中使用RxJava，当activity销毁的时候解除订阅
         */
        fun <T : ErrorBean> useInAppCompatActivityOnDestory(
            observable: Observable<T>,
            rxAppCompatActivity: RxAppCompatActivity,
            baseCallBack: RxJavaCallBack<T>
        ) {
            observable.compose(rxAppCompatActivity.bindUntilEvent(ActivityEvent.DESTROY))
                .observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(baseCallBack)
        }

        /**
         * 在Activity中使用RxJava，当activity销毁的时候解除订阅
         */
        fun <T : ErrorBean> useInActivityOnDestory(
            observable: Observable<T>,
            rxActivity: RxActivity,
            baseCallBack: RxJavaCallBack<T>
        ) {
            observable.compose(rxActivity.bindUntilEvent(ActivityEvent.DESTROY))
                .observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(baseCallBack)
        }

        /**
         * 在support包下的Fragment中使用RxJava进行网络请求，当Fragment销毁的时候解除订阅
         */
        fun <T : ErrorBean> useInSupportFragmentOnDestory(
            observable: Observable<T>,
            rxFragment: RxFragment,
            baseCallBack: RxJavaCallBack<T>
        ) {
            observable.compose(rxFragment.bindUntilEvent(FragmentEvent.DESTROY))
                .observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(baseCallBack)
        }

        /**
         * 在support包下的Fragment中使用RxJava进行网络请求，当Fragment销毁视图的时候解除订阅
         */
        fun <T : ErrorBean> useInSupportFragmentOnDestoryView(
            observable: Observable<T>,
            rxFragment: RxFragment,
            baseCallBack: RxJavaCallBack<T>
        ) {
            observable.compose(rxFragment.bindUntilEvent(FragmentEvent.DESTROY_VIEW))
                .observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(baseCallBack)
        }

        /**
         * 在app包下的Fragment中使用RxJava进行网络请求，当Fragment销毁的时候解除订阅
         */
        fun <T : ErrorBean> useInFragmentOnDestory(
            observable: Observable<T>,
            rxFragment: com.trello.rxlifecycle3.components.RxFragment,
            baseCallBack: RxJavaCallBack<T>
        ) {
            observable.compose(rxFragment.bindUntilEvent(FragmentEvent.DESTROY))
                .observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(baseCallBack)
        }

        /**
         * 在app包下的Fragment中使用RxJava进行网络请求，当Fragment销毁视图的时候解除订阅
         */
        fun <T : ErrorBean> useInFragmentOnDestoryView(
            observable: Observable<T>,
            rxFragment: com.trello.rxlifecycle3.components.RxFragment,
            baseCallBack: RxJavaCallBack<T>
        ) {
            observable.compose(rxFragment.bindUntilEvent(FragmentEvent.DESTROY_VIEW))
                .observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(baseCallBack)
        }

        /**
         * 在app包下的DialogFragment中使用RxJava进行网络请求
         */
        fun <T : ErrorBean> useInRxDialogFragment(observable: Observable<T>,
                                      rxDialogFragment: RxDialogFragment,
                                      baseCallBack: RxJavaCallBack<T>){
            observable.compose(rxDialogFragment.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(baseCallBack)
        }

        /**
         * 在app包下的DialogFragment中使用RxJava进行网络请求，当DialogFragment销毁视图的时候解除订阅
         */
        fun <T : ErrorBean> useInRxDialogFragmentOnDestoryView(observable: Observable<T>,
                                      rxDialogFragment: RxDialogFragment,
                                      baseCallBack: RxJavaCallBack<T>){
            observable.compose(rxDialogFragment.bindUntilEvent(FragmentEvent.DESTROY_VIEW))
                .observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(baseCallBack)
        }

        /**
         * 在support包下的DialogFragment中使用RxJava进行网络请求
         */
        fun <T : ErrorBean> useInSupportRxDialogFragment(observable: Observable<T>,
                                      rxDialogFragment: com.trello.rxlifecycle3.components.support.RxDialogFragment,
                                      baseCallBack: RxJavaCallBack<T>){
            observable.compose(rxDialogFragment.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(baseCallBack)
        }

        /**
         * 在support包下的DialogFragment中使用RxJava进行网络请求，当DialogFragment销毁视图的时候解除订阅
         */
        fun <T : ErrorBean> useInSupportRxDialogFragmentOnDestoryView(observable: Observable<T>,
                                                   rxDialogFragment: com.trello.rxlifecycle3.components.support.RxDialogFragment,
                                                   baseCallBack: RxJavaCallBack<T>){
            observable.compose(rxDialogFragment.bindUntilEvent(FragmentEvent.DESTROY_VIEW))
                .observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(baseCallBack)
        }
    }


}